<template>
	<view class="table">
		<view class="header">
			<view class="orderNum">
				序号
			</view>
			<view v-for="(item,index) in headList" :key="index" :style="{width:item.width}">
				{{item.title}}
			</view>
		</view>
		<view class="list">
		<scroll-view scroll-y="true" style="height: 100%;">
			<view v-for="(item,index) in tableData" :key="index"  class="row" >
			<view class="orderNum">
				{{(pageNum -1) * pageSize+(index + 1)}}
			</view>
			<view v-for="(val,par) in headList" :key="par" :style="{width:val.width}">
				<view >
				<template v-if="['alertStatus','reviewStatus','pickupStatus','nullState','visiteleaveStatus','recordStatus'].includes(val.props)">
					<view :style="{color:stateDeal(item[val.props],'color')}" @click="showDetail(val.props,item)">
						{{stateDeal(item[val.props],"label")}}
					</view>
				</template>
				<template v-else>
				<view class="mianContent">
				<view class="orderType" v-if="item.orderType && val.props === 'eatDate'">
					{{item.orderType}}
				</view><text>{{item[val.props]}}</text>		
				</view>

				</template>
				</view>
			</view>
			</view>
			<view class="emptyData" v-if="tableData.length === 0">
				<image src="../static/image/noData.png" mode=""></image>
				<text>暂无数据~~</text>
			</view>
		</scroll-view>
		</view>
		<view class="Pagination" v-if="paging">
		 <image src="../static/image/alarm/checkPage_left.png" mode="" @click="changePage('prev')"></image>
			<view class="pageToal">
				<text>{{currentPage}}</text>/<text>{{totalPage}}</text>
			</view>
		<image src="../static/image/alarm/checkPage_right.png" mode="" @click="changePage('next')"></image>
		</view>
	</view>
</template>

<script setup>
import {defineProps,watch,ref} from 'vue'
	const props = defineProps({
		showChecked:{
			type:Boolean,
			default:true
		},
		headList:{
			type:Array,
			default:()=>[

			]
		},
		tableData:{
			type:Object,
			default:()=>[

			]
		},
		pageType:{
			type:String,
			default:""
		},
		total:{
			type:Number,
			default:0
		},
		pageNum:{
			type:Number,
			default:1
		},
		pageSize:{
			type:Number,
			default:10
		},
		stateList:{
			type:Array,
			default:()=>[]
		},
		//是否分页
		paging:{
			type:Boolean,
			default:true
		}
	})
	//************************************* 处理分页数目************************************************************
	const currentPage = ref(1) //当前分页
	const totalPage = ref(1) //总页数
	watch([()=>props.pageNum,()=>props.pageSize,()=>props.total],(val)=>{
		currentPage.value = val[0]
		totalPage.value = Math.ceil(val[2] / val[1]) || 1
	},{
		deep:true,
		immediate:true
	})
	const stateDeal = (id,type)=>{
	return props.stateList.reduce((curr,val)=>{
			if(val.id == id){
				if(type === 'label'){
					curr +=  val.label
				}else if(type === 'color'){
					curr +=  val.color
				}else{
					curr +=  val.path
				}
			}
			return curr
		},"")
	}
	// 切换分页
	const emit = defineEmits(["checkPage","update:pageNum"])
	
	const changePage = (type)=>{
	let pageNumCopy = JSON.parse(JSON.stringify(props.pageNum))
	switch(type){
			case 'prev':
			if(pageNumCopy> 1){
			pageNumCopy--
			emit("update:pageNum",pageNumCopy)
			emit("checkPage")
			}
			break;
			case 'next':
			if(pageNumCopy < totalPage.value){
				pageNumCopy++
				emit("update:pageNum",pageNumCopy)
				emit("checkPage")
			}

			break
	}
	}
	//查看详情
	const showDetail = (props,item)=>{
		console.log("此时的老人id",item)
		let url = ""
		let elderId = ""
		const id = item[props]
		switch (props){
			case "alertStatus":
			const alertId = item.alertId
			url = `${stateDeal(id,"path")}?alertId=${alertId}`
			break;
			case "reviewStatus":
			const vid = item.id
			elderId = item.elderId
			
			url = `${stateDeal(id,"path")}?vid=${vid}&elderId=${elderId}`
			break;
			case "pickupStatus":
			elderId = item.elderId
			const drugApplyId = item.drugApplyId
			url = `${stateDeal(id,"path")}?drugApplyId=${drugApplyId}&elderId=${elderId}`
			break;
			case 'nullState':
			elderId = item.elderId
			const elderName = item.elderName
			url = `${stateDeal(id,"path")}?elderId=${elderId}&elderName=${elderName}`
			break;
			case 'visiteleaveStatus': //门卫访问区分
			let newvid = item.visitId
			if(item.visiteleaveStatus === "1"){
				newvid = item.id
			}
			elderId = item.elderId
			const visitInfoId = item.visitInfoId
			url = `${stateDeal(id,"path")}?vid=${newvid}&elderId=${elderId}&visitInfoId=${visitInfoId}&leavetype=${item.visiteleaveStatus}`
			break;
			case 'recordStatus':
			const detailId = item.id
			const recordStatus = item.recordStatus
			 elderId = item.elderId
			url = `${stateDeal(id,"path")}?detailId=${detailId}&recordStatus=${recordStatus}&elderId=${elderId}`
			
			
		}
			uni.navigateTo({
				url
			})
	}
</script>

<style lang="less" scoped>
	.table{
		border-radius: 10rpx;
		overflow: hidden;
		background-color: #f8fbff;
		border: 2rpx solid #ffffff;
		.checked{
			width: 8%;
			>image{
			width: 20rpx;
			height: 20rpx;	
			}
		
		}
		.header{
			background-color: #eceef9;
			font-weight: 500;
			font-size: 24rpx;
			color: #333333;
			display: flex;
			height: 68rpx;
			line-height: 68rpx;
			justify-content: space-between;
			padding: 0 20rpx;

			>view{
				text-align: center;
			}
		}
		.list{
			height: 100%;
			min-height: 800rpx;
			width: 100%;
			padding: 0 20rpx;
			.row{
				width: 100%;
				display: flex;
				font-weight: 400;
				font-size: 20rpx;
				color: #666666;
				padding: 20rpx 0;
				border-bottom: 1rpx solid #e5e5e5;
				>view{
					text-align: center;
				}
			}
		}
		.Pagination{
			display: flex;
			margin: 74rpx 0 60rpx;
			justify-content: center;
			>image{
				width: 128rpx;
				height: 44rpx;
			}
			.pageToal{
				margin: 0 108rpx;
				color: #333333;
				font-size: 28rpx;
				text{
					&:first-child{
						color: #0379FF;
					}
				}
			}
		}
	}
	.orderNum{
		width: 10%;
	}
	.mianContent{
		display: flex;
		justify-content: center;
		align-items: center;
		.orderType{
			font-weight: 400;
			color: #2778FF;
			background-color: #ECEEF9;
			padding: 3rpx 6rpx;
			border-radius: 4rpx;
			margin-right: 15rpx;
		}
	}
	.emptyData{
		margin-top: 100rpx;
		display: flex;
		justify-content: center;
		flex-direction: column;
		align-items: center;
		>image{
			width: 300rpx;
			height: 250rpx;
		}
		text{
			font-size: 28rpx;
			color: #767676;
		}

	}
	@media screen and (min-width: 768px){
		.table{
			.header{
				font-size: 50rpx;
				height: 100rpx;
				line-height: 100rpx;
			}
			.list{
				.row{
					font-size: 48rpx;
					line-height: 80rpx;
				}
			}
			.Pagination{
				font-size: 45rpx;
				>image{
					width: 248rpx;
					height: 88rpx;
				}
				.pageToal{		
					font-size: 58rpx;
				}
			}
		}
		.emptyData{
			>image{
				width: 600rpx;
				height: 500rpx;
			}
			text{
				font-size: 58rpx;
			}

		}
	}
</style>